/*
 * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
 */
package org.emergent.antbite.savant.ant;


import org.apache.tools.ant.Project;

import org.emergent.antbite.savant.log.Log;
import org.emergent.antbite.savant.log.LogListener;


/**
 * <p>
 * This is the {@link LogListener} used specifically for logging
 * via Ant's Project object.
 * </p>
 *
 * @author Brian Pontarelli
 */
public class AntLogListener implements LogListener {

    private Project project;


    /**
     * Constructs a AntLogListener using the given Ant Project.
     *
     * @param   project The Ant Project to log to.
     */
    public AntLogListener(Project project) {
        this.project = project;
    }


    /**
     * Handles a log message and level.
     *
     * @param msg The message to be logged.
     * @param level The level (any value is acceptable here) to log at.
     */
    public void logged(String msg, String level) {
        int antLevel;
        if (level.equals(Log.VERBOSE)) {
            antLevel = Project.MSG_VERBOSE;
        } else if (level.equals(Log.DEBUG)) {
            antLevel = Project.MSG_DEBUG;
        } else if (level.equals(Log.INFO)) {
            antLevel = Project.MSG_INFO;
        } else if (level.equals(Log.WARN)) {
            antLevel = Project.MSG_WARN;
        } else if (level.equals(Log.ERROR)) {
            antLevel = Project.MSG_ERR;
        } else if (level.equals(Log.FATAL)) {
            antLevel = Project.MSG_ERR;
        } else {
            throw new IllegalArgumentException("Invalid log level [" + level + "]");
        }

        project.log(msg, antLevel);
    }
}
